package com.handtransaction.service.impl;

import com.handtransaction.dao.DataDao;
import com.handtransaction.entity.DataBean;
import com.handtransaction.entity.Goods;
import com.handtransaction.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataStatisticsServiceImpl implements DataStatisticsService {
    @Autowired
    private DataDao dataDao;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OffShelfService offShelfService;
    @Autowired
    private  TipOffService tipOffService;


    //展示前6天的数据
    @Override
    public List<DataBean> showNew7DayData() {
        List<DataBean> var1 = dataDao.queryAllData();
        List<DataBean> dataBeanList = new ArrayList<>();
        // get the current date
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM月dd日");
        //创建当天的数据bean
        DataBean todayDataBean = new DataBean(userService.readAllUser().size(),
                goodsService.QueryGoods().size(),
                orderService.queryAllOrder().size(),
                tipOffService.showAllTOGoods().size(),
                offShelfService.queryAllOSGoods().size(),
                orderService.showTransactionAmount(),
                localDate.format(formatter));
        //数据库数据量大于等于6则获取最新6条记录
        if(var1.size() >= 6 ){
            for(int i=var1.size()-6,j = 0;i<var1.size();++i,++j){
                var1.get(i).setDate(localDate.plusDays(-6+j).format(formatter));
                dataBeanList.add(var1.get(i));
            }
            dataBeanList.add(todayDataBean);
            return dataBeanList;
        }else{
            //小于6则获取全部，剩余不足的用0填充
            boolean flag = true;
            for(int i= 0;i<7;++i){
                if(var1.size()>i){
                    var1.get(i).setDate(localDate.plusDays(-var1.size()+i).format(formatter));
                    dataBeanList.add(var1.get(i));
                }else{
                    if(flag){
                        dataBeanList.add(todayDataBean);
                        flag = false;
                    }else{
                        dataBeanList.add(new DataBean(0,0,0,0,0,0));
                    }
                }
            }
            return dataBeanList;
        }
    }

    //展示各项指标数据
    @Override
    public Map<String, Object> showAllIndex() {
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("userNumber" , userService.readAllUser().size());
        dataMap.put("goodsNumber" , goodsService.QueryGoods().size());
        dataMap.put("tipOffNumber" , tipOffService.showAllTOGoods().size());
        dataMap.put("orderNumber" , orderService.queryAllOrder().size());
        dataMap.put("offShelfNumber" , offShelfService.queryAllOSGoods().size());
        dataMap.put("transactionNumber" , orderService.showTransactionAmount());
        return dataMap;
    }

    //展示商品数量最多的前10种商品类型
    @Override
    public List<Goods> showMostTenCategory(){
        List<Goods> list = dataDao.queryMostTenCategory();
        while (list.size()<10){
            list.add(new Goods(0,""));
        }
        return list;
    }

    //展示已发生交易的商品类型
    @Override
    public List<Goods> showAllTransactionCategory(){
        return dataDao.queryOrderCategory();
    }

    //定时记录数据
    @Override
    public int saveData() {
        DataBean todayDataToSave = new DataBean(userService.readAllUser().size(),
                goodsService.QueryGoods().size(),
                orderService.queryAllOrder().size(),
                tipOffService.showAllTOGoods().size(),
                offShelfService.queryAllOSGoods().size(),
                orderService.showTransactionAmount());
        return dataDao.insertNewData(todayDataToSave);
    }



}
